<script setup lang="ts">
import { provide, watch, onMounted } from 'vue'
import { propTypes } from '@/utils/propTypes'
import { type ComponentSize, ElConfigProvider } from 'element-plus'
import { useWindowSize } from '@vueuse/core'
import { useAppStore } from '@/stores/app'
import { setCssVar } from '@/utils'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

const appStore = useAppStore()

const props = defineProps({
  size: propTypes.oneOf<ComponentSize>(['default', 'small', 'large']).def('default')
})

provide('configGlobal', props)

// 初始化所有主题色
onMounted(() => {
  appStore.setCssVarTheme()
})

const { width } = useWindowSize()

// 监听窗口变化
watch(
  () => width.value,
  (width: number) => {
    if (width < 768) {
      !appStore.getMobile ? appStore.setMobile(true) : undefined
      setCssVar('--left-menu-min-width', '0')
      appStore.setCollapse(true)
      appStore.getLayout !== 'classic' ? appStore.setLayout('classic') : undefined
    } else {
      appStore.getMobile ? appStore.setMobile(false) : undefined
      setCssVar('--left-menu-min-width', '64px')
    }
  },
  {
    immediate: true
  }
)
</script>

<template>
  <ElConfigProvider namespace="el" :message="{ max: 1 }" :size="size" :locale="zhCn">
    <slot></slot>
  </ElConfigProvider>
</template>
